<template>
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        v-model="baTable.table.extend!.showExportDialog"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ $t('shop.order.order.export') }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="warn-tip">
                <el-alert type="warning" :closable="false" show-icon title="保存后自动生成导出记录，可在订单导出列表页查看和下载文件" />
            </div>
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    :model="exportForm"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-loading="baTable.form.loading"
                >
                    <FormItem
                        :label="$t('shop.order.order.export_name')"
                        type="string"
                        v-model="exportForm.name"
                        prop="name"
                        :placeholder="$t('Please input field', { field: $t('shop.order.order.export_name') })"
                    />

                    <InputNumber
                        :label="$t('shop.order.order.export_max_row_num')"
                        v-model.number="exportForm.export_max_row_num"
                        prop="export_max_row_num"
                        :placeholder="$t('Please input field', { field: $t('shop.order.order.export_max_row_num') })"
                        :input-attr="{
                            min: 1000,
                            max: 10000,
                        }"
                    />

                    <el-col :xs="24" :sm="24" :lg="24">
                        <el-scrollbar>
                            <el-form-item :label="$t('shop.order.order.export_fields')" prop="export_fields">
                                <el-checkbox-group v-model="exportForm.export_fields">
                                    <div class="export-fields-list">
                                        <el-col v-for="(item, index) in exportTableField" :key="index" :span="6" class="export-fields-list-item">
                                            <el-checkbox :label="item" :value="item" :checked="true">
                                                {{ item.name }}
                                            </el-checkbox>
                                        </el-col>
                                    </div>
                                </el-checkbox-group>
                            </el-form-item>
                        </el-scrollbar>
                    </el-col>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.table.extend!.showExportDialog = false">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="onSubmit(formRef)" type="primary">
                    {{ t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { type FormInstance, type FormItemRule } from 'element-plus'
import { inject, onMounted, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import { buildValidatorData } from '/@/utils/validate'
import { getTableHeadField } from '/@/api/backend/shop/order/export'
import type baTableClass from '/@/utils/baTable'
import { createExportTask } from '/@/api/backend/shop/order/order'
import InputNumber from '/@/components/shop/form/inputNumber.vue'

const config = useConfig()
const formRef = ref<FormInstance>()
const { t } = useI18n()

const baTable = inject('baTable') as baTableClass

const exportForm = reactive<{
    name: string
    export_fields: any[]
    export_max_row_num: number
    total: number
}>({
    name: '',
    export_fields: [],
    export_max_row_num: 10000,
    total: 0,
})

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    name: [buildValidatorData({ name: 'required', title: t('shop.order.order.export_name') })],
    export_fields: [buildValidatorData({ name: 'required', title: '', message: '请勾选导出字段' })],
    export_max_row_num: [buildValidatorData({ name: 'required', title: t('shop.order.order.export_max_row_num') })],
})

const onSubmit = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            exportForm.export_fields = exportTableField.value.filter((item) => exportForm.export_fields.includes(item))
            exportForm.total = baTable.table.total!
            const params = Object.assign(baTable.table.filter!, exportForm)
            createExportTask(params)
                .then(() => {
                    baTable.table.extend!.showExportDialog = false
                    baTable.onTableHeaderAction('refresh', {})
                })
                .finally(() => {
                    baTable.form.submitLoading = false
                })
        }
    })
}

const exportTableField = ref<
    {
        column: string
        name: string
    }[]
>([])

onMounted(() => {
    baTable.form.loading = true
    getTableHeadField()
        .then((res) => {
            baTable.form.loading = false
            exportTableField.value = res.data
        })
        .catch(() => {
            baTable.form.loading = false
        })
})
</script>

<style scoped lang="scss">
.warn-tip {
    padding-top: 10px;

    text {
        font-size: 16px;
    }
}
.action {
    display: flex;
    justify-content: center;
    padding-top: 10px;
    padding-bottom: 10px;
}

.export-fields-list {
    display: flex;
    flex-wrap: wrap;
}

.export-fields-list-item {
    flex-basis: calc(25% - 5px);
    margin-bottom: 10px;
    min-width: 150px;
}
</style>
